package com.xiaomazi.acl.controller;

import com.xiaomazi.acl.dto.UserDTO;
import com.xiaomazi.acl.entity.User;
import com.xiaomazi.acl.service.RoleService;
import com.xiaomazi.acl.service.UserService;
import com.xiaomazi.common.utils.MD5;
import com.xiaomazi.common.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @program: llxy-parent
 * @description: 用户Handler
 * @author: 小马子
 * @create: 2023-03-31 20:19
 **/
@RequestMapping("/admin/acl/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;


    @ApiOperation(value = "获取管理用户分页列表")
    @GetMapping("/")
    public R index(@RequestBody UserDTO userDTO){
        return R.ok().data(userService.selectUserByCondition(userDTO));
    }

    @ApiOperation(value = "新增管理用户")
    @PostMapping("/save")
    public R save(@RequestBody User user) {
        user.setPassword(MD5.encrypt(user.getPassword()));
        userService.save(user);
        return R.ok();
    }

    @ApiOperation(value = "修改管理用户")
    @PutMapping("update")
    public R updateById(@RequestBody User user) {
        userService.updateById(user);
        return R.ok();
    }

    @ApiOperation(value = "删除管理用户")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        userService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "根据id列表删除管理用户")
    @DeleteMapping("/batchRemove")
    public R batchRemove(@RequestBody List<String> idList){
        userService.removeByIds(idList);
        return R.ok();
    }

    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public R toAssign(@PathVariable String userId){
        return R.ok().data(roleService.findRoleByUserId(userId));
    }

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public R doAssign(@RequestParam String userId,@RequestParam String[] roleId){
        roleService.saveUserRoleRealtionShip(userId,roleId);
        return R.ok();
    }


}
